import IEntitySystem from "../../base/ECSR/IEntitySystem";
import MovePathComponent from "../components/MovePathComponent";
import Guid from "../../../common/utils/Guid";
import AddBubbleComponent from "../components/AddBubbleComponent";

export default class MovePathSystem extends IEntitySystem
{
    constructor(world)
    {
        super(world);
    }
    // 每逻辑帧调用
    Execute()
    {
        let movePathComList = this.world.GetComponents("MovePathComponent") as Array<MovePathComponent>;
        if(movePathComList === null) return;

        let removePathComList = [];

        for(let i = 0; i < movePathComList.length; ++i)
        {
            let movePathCom = movePathComList[i];

            // 有移动路径
            if(movePathCom.path.length >= 2)
            {
                if(movePathCom.currPathStartIndex == 0 && movePathCom.timeElapse === 0)
                {
                    let startPos = movePathCom.path[movePathCom.currPathStartIndex];
                    let endPos = movePathCom.path[movePathCom.currPathStartIndex + 1];
                    let time = endPos.sub(startPos).len() / movePathCom.speed;
                    // 不求精确, 按照一个完整的逻辑帧移动,向下取整
                    movePathCom.time = Math.floor(time);
                    cc.log("MovePathSystem| startPos="+ startPos.toString() + ",endPos=" + endPos.toString() +" time=" + movePathCom.time);
                }

                // 已经走完了
                if(movePathCom.timeElapse >= movePathCom.time)
                {
                    movePathCom.timeElapse = 0;
                    movePathCom.time = 0;

                    movePathCom.currPathStartIndex += 1;
                    if(movePathCom.currPathStartIndex >= movePathCom.path.length)
                    {
                        removePathComList.push(movePathCom);
                    }
                    // 达到终点
                    else if(movePathCom.currPathStartIndex >= movePathCom.path.length - 1)
                    {
                        movePathCom.time = 3;  // 1s,用来做调整位置运动
                    }
                    else
                    {
                        let startPos = movePathCom.path[movePathCom.currPathStartIndex];
                        let endPos = movePathCom.path[movePathCom.currPathStartIndex + 1];
                        let time = endPos.sub(startPos).len() / movePathCom.speed;
                        // 不求精确, 按照一个完整的逻辑帧移动,向下取整
                        movePathCom.time = Math.floor(time);
                    }
                }
            }

            movePathCom.timeElapse += 1;
        }
        // 删除movePathCom, 并创建一个AddBubbleComponent
        removePathComList.forEach(com => {
            this.world.RemoveComponent(com);
            let id = Guid.guid();
            let addBubbleEntity = this.world.AddEntity(id);
            var addBubbleCom = new AddBubbleComponent();
            addBubbleEntity.AddComponent(addBubbleCom);
            addBubbleCom.bubbleId = (com as MovePathComponent).entityId;
        });
    }
}